package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.*;
import com.sky.entity.Category;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.EmployeeMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class CtegoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public PageResult pageFind(CategoryPageQueryDTO categoryPageQueryDTO) {
        //开启分页查询，指定页码和查询数量
        PageHelper.startPage(categoryPageQueryDTO.getPage(),
                categoryPageQueryDTO.getPageSize());
        Page<Category> List= categoryMapper.pageQuery(categoryPageQueryDTO);
        //将结果封装为pageResult对象返回
        //employeeList.getTotal()得到查询的数量，employeeList.getResult()得到查询结果集合
        return new PageResult(List.getTotal(),List.getResult());
    }

    @Override
    public Category findById(Long id) {
        Category category= categoryMapper.findById(id);
        return category;
    }

    @Override
    public void changeStatus(Long id, Integer status) {
        Category category = new Category();
        category.setId(id);
        category.setStatus(status);
        /*
        使用aop进行自动填充
        category.setUpdateUser(BaseContext.getCurrentId());*/
        categoryMapper.update(category);
    }

    @Override
    public void updateinfo(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        /*
        使用aop进行自动填充
        category.setUpdateUser(BaseContext.getCurrentId());*/
        categoryMapper.update(category);
    }

    @Override
    public void add(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        /*
        使用aop进行自动填充
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setCreateUser(BaseContext.getCurrentId());*/
        category.setStatus(StatusConstant.ENABLE);
        categoryMapper.add(category);
    }

    @Override
    public void delById(Long id) {
        //判断是否有菜品关联此分类，有则不能删除，抛出异常终止程序
        int dishCount =dishMapper.getCountByCategory(id);
        if(dishCount>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //判断是否有套菜关联此分类，有则不能删除，抛出异常终止程序
        int setmealCount =setmealMapper.getCountByCategory(id);
        if(setmealCount>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //既无菜品关联，又无套菜关联则进行删除
        categoryMapper.delByid(id);
    }

    @Override
    public List<Category> listGetByType(Integer type) {
        if(type==null){
            type=1;
        }
        return categoryMapper.listGetByType(type);
    }

}
